﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Xww.WindowsForm.Controls{
    public class TextBoxFileName : TextBox {
        public ErrorProvider ErrorProviderControl {
            get;
            set;
        }
        string fileNotExistText = "File not exist, please check !";
        public string FileNotExistText {
            get { return fileNotExistText; }
            set { fileNotExistText = value; }
        }

        public TextBoxFileName()
            : base() {
            this.AllowDrop = true;
            this.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            this.AutoCompleteSource = AutoCompleteSource.AllSystemSources;
        }
        public virtual bool CheckFileExist() {
            string trimmedText = this.Text.Trim();
            bool rst = System.IO.File.Exists(trimmedText);
            if (ErrorProviderControl != null) {
                if (rst) {
                    ErrorProviderControl.SetError(this, "");
                }
                else {
                    ErrorProviderControl.SetError(this, FileNotExistText);
                }
            }
            this.Text = trimmedText;
            return rst;
        }

        protected override void OnDragEnter(DragEventArgs drgevent) {
            base.OnDragEnter(drgevent);
            drgevent.Effect = DragDropEffects.All;
        }
        protected override void OnDragDrop(DragEventArgs drgevent) {
            base.OnDragDrop(drgevent);
            string[] fileNames = (string[])drgevent.Data.GetData(DataFormats.FileDrop);
            if (fileNames.Length > 0) {
                this.Text = fileNames[0];
            }
        }
    }
}
